
import { apiHttp } from '../apiHttp';
import Api from './api';
import * as Interface from './interface';
import { sortByLevel } from '../../libs/utils';
import ServiceApi from '../../api/common/commonController.api';
import MenuApi from '../../api/system/sysMenuController.api';
import { JsonResult, DictionariesVo, SysMenuTree, ComManagerDto, TagsVo } from '../../api/Interface';
import CompanyApi from '../../api/company/companyController.api'; // 企业
import ManageApi from '../../api/company/comManagerController.api'; // 高管信息
import PersonApi from '../../api/common/commonController.api'; // 人物职务
import TagApi from '../../api/company/tagsController.api'; // 标签
import CommonApi from '../../api/common/commonController.api'; // 标签
import PersonSearchApi from '../../api/person/personController.api';

export default {
    // 登录接口
    login: async (loginFrom: Interface.LoginForm) => {
        const res: any = await apiHttp(Api.LOGIN, loginFrom);
        return res;
    },
    // 获取文件上传授权
    getPolicy: async () => {
        const res: {data: string} = await apiHttp(Api.GET_POLICY);
        return res.data;
    },
    // 获取字典方法
    getDictionaries: async (): Promise <DictionariesVo>  => {
        const res: JsonResult = await apiHttp(ServiceApi.getDictionaries);
        return res.data;
    },
    // 获取菜单栏
    getSysMenuTree: async (): Promise <SysMenuTree[]> => {
        const { data }: JsonResult = await apiHttp(MenuApi.getSysMenuTree);
        // 根据level排序
        const newMenuList = sortMenu(data);
        return newMenuList;
    },
    // 搜索企业库 yyr
    getCompanySearch: async (companyOpenSearch: Interface.CompanySearch): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(CompanyApi.getCompanyFromOpenSearch, companyOpenSearch);
        return res;
    },
    // 获取标签列表 yyr
    getTagList: async (): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(TagApi.getTagsList, '');
        return res;
    },
    // 根据标签类型获取标签列表
    getTagListByType: async (type: {tagType: string}): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(TagApi.getTagsByType, type);
        return res;
    }, 
    // 根据行业类型获取标签列表
    getTagsByIndustryId: async (industryId: string): Promise<TagsVo[]> => {
        const res: JsonResult = await apiHttp(TagApi.getTagsByIndustryId, {industryId});
        return res.data;
    }, 
    // 模糊搜索标签
    tagSearch: async (name: {tagName: string}): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(TagApi.tagSearch, name);
        return res;
    }, 
    // 获取高管信息 yyr
    getManageTeam: async (param: Interface.ManageSearch): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(ManageApi.comManagerPage, param);
        return res;
    },
    // 添加高管信息 yyr
    saveManageTeam: async (param: ComManagerDto): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(ManageApi.saveComManager, param);
        return res;
    },
    // 编辑高管信息 yyr
    updateManageTeam: async (param: ComManagerDto): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(ManageApi.updateComManager, param);
        return res;
    },
    // 删除高管信息 yyr
    deleteManage: async (manageid: number): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(ManageApi.deleteComManager, {id: manageid});
        return res;
    },
    // 查询人物 
    getPerson: async (param: Interface.PersonSearchDto): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(PersonSearchApi.personPage, param);
        return res;
    },
    // 获取人物职务列表 yyr
    getPersonPosition: async (tit: string): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(PersonApi.getDutiesLevel, {title: tit});
        return res;
    },
    // 根据url自动下载文件 yyf
    automaticUploadFile: async (fileName: string, fileUrl: string): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(CommonApi.automaticUploadFile, {fileName, fileUrl});
        return res;
    },
    // 根据事件id、业务分类获取操作记录信息
    getMessageOperate: async (operateHistoryDto: Interface.OperateHistoryDto): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(CommonApi.getMessageOperate, operateHistoryDto);
        return res;
    },
    // 根据事件id、业务分类获取发布记录信息
    getMessagePublish: async (operateHistoryDto: Interface.OperateHistoryDto): Promise<JsonResult> => {
        const res: JsonResult = await apiHttp(CommonApi.getMessagePublish, operateHistoryDto);
        return res;
    },
};
// 排序方法
const sortMenu = (menuList) => {
    const newMenu =  menuList.map((item) => {
        if (item.children && item.children.length) {
            item.children.sort(sortByLevel);
            item.children.map(x => {
                if (x.children && x.children.length) {
                    x.children.sort(sortByLevel);
                }
            });
        }
        return item;
    });
    newMenu.sort(sortByLevel);
    return newMenu;
};
